// 99 乘法表
let row = 9;

for (let index = 1; index <= row; index++) {
    let str = "";
    for (let j = 1; j <= index; j++) {
        str += `${j} * ${index} = ${j * index} \t`;
    }
    console.log(`${str}`);
}

// 斐波那契数列(兔子数列)
let num = 10;

let arr = [1, 1];

for (let index = 2; index < num; index++) {
    arr.push(arr[index - 1] + arr[index - 2]);
}

console.log(arr);


// 排序
let nums = [3, 5, 2, 4, 16, 71, 28, 39, 10];

// 冒泡
for (let i = 0; i < nums.length - 1; i++) {
    for (let j = 0; j < nums.length - 1 - i; j++) {
        if (nums[j] > nums[j + 1]) {
            // 交换位置
            let temp = nums[j];
            nums[j] = nums[j + 1];
            nums[j + 1] = temp;
        }
    }
}

console.log(nums);

let n = 10;

// 二分查找
function binarySearch(nums, n) {
    let right = nums.length - 1;
    let left = 0;
    let index = -1;

    while (left < right) {
        let middle = (left + right) >> 1;
        if (nums[middle] == n) {
            index = middle;
            break;
        } else if (nums[middle] > n) {
            right = middle - 1;
        } else {
            left = middle + 1;
        }
    }
    return index;
}

function binarySearch2(nums, n, left, right) {
    if (left > right) {
        return -1;
    }
    // Math.ceil() Math.floor() Math.round() parseInt()
    let middle = (left + right) / 2;
    if (nums[middle] == n) {
        return middle;
    } else if (nums[middle] > n) {
        return binarySearch2(nums, n, left, middle - 1);
    } else {
        return binarySearch2(nums, n, middle + 1, right);
    }
}

let i = binarySearch2(nums, 4, 0, nums.length);
console.log(i);


